package com.jlastu.dlxy_androidapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.jlastu.dlxy_androidapp.R;
import com.jlastu.dlxy_androidapp.entity.OrderCart;

import java.util.List;

public class DetailsOrderAdapter extends BaseAdapter {
    private List<OrderCart> orderList;
    private LayoutInflater inflater;
    private OnTotalAmountChangeListener listener;

    private double totalAmount;

    public DetailsOrderAdapter(Context context, List<OrderCart> orderList, OnTotalAmountChangeListener listener) {
        this.orderList = orderList;
        this.inflater = LayoutInflater.from(context);
        this.totalAmount = calculateTotalAmount(orderList);
        this.listener=listener;
    }
    private void notifyTotalAmountChange() {
        if (listener != null) {
            double total = calculateTotal();
            listener.onTotalAmountChanged(total);
        }
    }

    // 计算选中订单项的总价
    private double calculateTotal() {
        double total = 0;
        for (OrderCart order : orderList) {
            if (order.isSelected()) {
                total += order.getTotalAmount();
            }
        }
        return total;
    }
    @Override
    public int getCount() {
        return orderList.size();
    }

    @Override
    public Object getItem(int position) {
        return orderList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.cart_list, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.locateView=convertView.findViewById(R.id.locate);
            viewHolder.productNameTextView = convertView.findViewById(R.id.name);
            viewHolder.productDetailsTextView = convertView.findViewById(R.id.detail);
            viewHolder.quantityTextView = convertView.findViewById(R.id.quantity);
            viewHolder.totalPriceTextView = convertView.findViewById(R.id.price);
            viewHolder.orderTimeTextView = convertView.findViewById(R.id.time);
            viewHolder.img_item1=convertView.findViewById(R.id.img_item1);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        OrderCart order = orderList.get(position);
        viewHolder.locateView.setText(order.getLocate());
        viewHolder.productNameTextView.setText(order.getProductName());
        viewHolder.productDetailsTextView.setText(order.getProductDetails());
        viewHolder.quantityTextView.setText(String.valueOf(order.getQuantity()));
        viewHolder.totalPriceTextView.setText(String.valueOf(order.getTotalAmount()));
        viewHolder.orderTimeTextView.setText(order.getOrderTime());
        CheckBox selectRadioButton = convertView.findViewById(R.id.selectRadioButton);
        selectRadioButton.setChecked(orderList.get(position).isSelected());
//        判定

        if(order.id==1){
            viewHolder.img_item1.setImageResource(R.drawable.apple);
        }else viewHolder.img_item1.setImageResource(R.drawable.watermelon);

        selectRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                order.setSelected(isChecked);
                System.out.println("order:"+order.isSelected());
                if (listener != null) {
                    double total = calculateTotal();
                    listener.onTotalAmountChanged(total);
                }
                orderList.get(position).setSelected(isChecked);
                if (allCheckboxChangeListener != null) {
                    allCheckboxChangeListener.onAllCheckboxStateChanged();
                }
            }
        });

        return convertView;
    }
    public interface OnTotalAmountChangeListener {
        void onTotalAmountChanged(double totalAmount);
    }
    @Override
    public void notifyDataSetChanged() {
        // 这里可以添加额外的逻辑，比如保存数据等
        super.notifyDataSetChanged();
    }

    public void setAllSelected(boolean isSelected) {
        for (OrderCart order : orderList) {
            order.setSelected(isSelected);
        }
        notifyDataSetChanged(); // 通知数据集改变，视图将刷新
    }
    public interface OnAllCheckboxStateChangeListener {
        void onAllCheckboxStateChanged();
    }

    private OnAllCheckboxStateChangeListener allCheckboxChangeListener;

    // 设置回调接口的实例
    public void setOnAllCheckboxStateChangeListener(OnAllCheckboxStateChangeListener listener) {
        this.allCheckboxChangeListener = listener;
    }

    private static class ViewHolder {
        TextView locateView;
        TextView productNameTextView;
        TextView productDetailsTextView;
        TextView quantityTextView;
        TextView totalPriceTextView;
        TextView orderTimeTextView;
        ImageView img_item1;
    }

    private double calculateTotalAmount(List<OrderCart> orderList) {
        double totalAmount = 0;
        for (OrderCart order : orderList) {
            totalAmount += order.getTotalAmount();
        }
        return totalAmount;
    }

    public double getTotalAmount() {
        return totalAmount;
    }
}